package com.nfit.shopadmin.controller;

import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.digest.MD5;
import com.nfit.shopadmin.service.UserService;
import com.nfit.shopmodel.dto.user.UserInfoDto;
import com.nfit.shopmodel.entity.user.UserInfo;
import com.nfit.shopmodel.entity.wechat.WechatLoginDto;
import com.nfit.shopmodel.vo.admin.LoginVo;
import com.nfit.shopmodel.vo.common.Result;
import com.nfit.shopmodel.vo.common.ResultCodeEnum;
import com.nfit.shopmodel.vo.h5.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:UserController
 * Package:com.nfit.shopadmin.controller
 * Description
 *
 * @Author L
 * @Create 2024/12/26 19:23
 */
@Slf4j
@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 微信一键登入，未实现
     *
     * @param code 微信登录临时凭证 code
     * @param encryptedData 加密的用户数据
     * @param iv 偏移量
     */
    @GetMapping("/weChatLogin")
    public Result weChatLogin(@RequestParam("code") String code,
                              @RequestParam("encryptedData") String encryptedData,
                              @RequestParam("iv") String iv){
        UserInfo userInfo = userService.wechatLogin(code,encryptedData,iv);
        return Result.build(userInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 手机号码登录
     * @param wechatLoginDto 接收前端专递的数据
     */
    @PostMapping("/wechatLoginPhone")
    public Result wechatLoginPhone(@RequestBody WechatLoginDto wechatLoginDto){
        UserInfo userInfo = userService.wechatLoginPhone(wechatLoginDto);
        return Result.build(userInfo,ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询用户的信息
     */
    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestParam("phone") String phone){
        UserInfoVo vo = userService.getUserInfo(phone);
        return Result.build(vo,ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改个人信息
     * @param userInfoDto 接收前端的数据
     */
    @PutMapping("/update")
    public Result update(@RequestBody UserInfoDto userInfoDto){
//        log.info("个人信息：{}",userInfoDto);
        userService.update(userInfoDto);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
}
